<?php
namespace app\common\model;

use think\Model;

class Stat extends Model
{
    protected $connection = 'db_config_aiqi';

    // 查询最新统计日期
    public function getlast(){
        return $this->order('id desc')->find();
    }
    /**
     * 获取最后统计日期
     * @return array|null
     */
    public function getLastStatDate()
    {
        return $this->order('stat_date DESC')->find();
    }

    public function getStatList($map, $page=1, $row=15, $order='id desc'){
        $list = $this->where($map)
            ->order($order)
            ->paginate($row);
        return $list;
    }

    /**
     * 今日排行前三的游戏
     * @param $map
     * @param $order
     * @return mixed
     */
    public static function getStatGameList($map,$order='id desc', $limit=3){
        $list = self::field('gid, sum(new_users) as new_users, sum(new_roles) as new_roles,
            sum(active_users) as active_users, sum(pay_amount) as pay_amount, sum(pay_users) as pay_users, sum(pay_times) as pay_times')
            ->where($map)
            ->order($order)
            ->group('gid')
            ->limit($limit)
            ->select()->toArray();

        return $list;
    }

    /**
     * 今日排行前三的游戏
     * @param $map
     * @param $order
     * @return mixed
     */
    public static function getDayList($map){
        $list = self::field('gid,stat_date, sum(new_users) as new_users, sum(new_roles) as new_roles,
            sum(active_users) as active_users, sum(pay_amount) as pay_amount, sum(pay_users) as pay_users, sum(pay_times) as pay_times')
            ->where($map)
            ->group('stat_date')
            ->select()->toArray();

        return $list;
    }

    public function getStatDayList($map, $page=1, $row=15, $order='id desc'){
        $list = $this->field('stat_date, sum(new_users) as new_users, sum(new_roles) as new_roles,
            sum(active_users) as active_users, sum(pay_amount) as pay_amount, sum(pay_users) as pay_users, sum(pay_times) as pay_times,
            ROUND(SUM(pay_amount)/NULLIF(SUM(active_users), 0), 2) AS arpu,
            ROUND(SUM(pay_amount)/NULLIF(sum(pay_users), 0), 2) AS arppu')
            ->where($map)
            ->order($order)
            ->group('stat_date')
            ->paginate($row, false, ['page' => $page,'query' =>request()->param()]);
        return $list;
    }
    /**
     * 插入统计记录（用于非当天数据）
     * @param array $data 统计数据
     * @return bool
     */
    public function insertStat(array $data)
    {
        // 检查是否已存在相同日期的记录
        $exists = $this->where([
            'cid' => $data['cid'],
            'gid' => $data['gid'],
            'stat_date' => $data['stat_date']
        ])->count();

        if (!$exists) {
            // 只有不存在时才插入
            return $this->insert($data);
        }

        return true;
    }

    public function updateStat(array $data)
    {
        // 查找是否已存在相同日期的记录
        $exists = $this->where([
            'cid' => $data['cid'],
            'gid' => $data['gid'],
            'stat_date' => $data['stat_date']
        ])->find();

        if ($exists) {
            // 更新现有记录
            return $this->where('id', $exists['id'])->update($data);
        } else {
            // 插入新记录
            return $this->insert($data);
        }
    }

}